package com.bnzj.core.rest;

import com.bnzj.core.exception.BusinessException;
import org.springframework.util.Assert;

/**
 * @author barry
 * @param <T>
 *
 */
public class ResponseResult<T> implements IResult<T> {
	private static final long serialVersionUID = -7792279970568202800L;
	private ResultCode resultCode;
	private T data;
    
    public static <T> ResponseResult<T> success(){
        return success(null);
    }
	
	public static <T> ResponseResult<T> success(T t){
		return new ResponseResult<T>(ResultCode.SUCCEED, t);
	}

	public static <T> ResponseResult<T> bizException(BusinessException e){
	    return new ResponseResult<T>(e.getResultCode());
    }
    
    public static <T> ResponseResult<T> failure(int code){
        return new ResponseResult<T>(code, null);
    }
    
    public static <T> ResponseResult<T> failure(int code, T t){
        return new ResponseResult<T>(code, t);
    }
    
    public static <T> ResponseResult<T> failure(int code, String msg, T t){
        return failure(new ResultCode(code, msg), null);
    }
    
    public static <T> ResponseResult<T> failure(ResultCode resultCode){
        return failure(resultCode, null);
    }
    
    public static <T> ResponseResult<T> failure(ResultCode resultCode, T t){
        return new ResponseResult<T>(resultCode, t);
    }
    
    public ResponseResult(ResultCode resultCode, T data) {
        Assert.notNull(resultCode, "resultCode can not null");
        this.resultCode = resultCode;
        this.data = data;
    }

    public ResponseResult(int code, T data) {
        this(ResultCode.getResultCode(code), data);
    }

    public ResponseResult(){}
    
    public ResponseResult(ResultCode resultCode) {
        this(resultCode, null);
    }
    
    public ResponseResult(int code) {
        this(code, null);
    }

	@Override
	public int getCode() {
		return resultCode.getCode();
	}

	@Override
	public String getMsg() {
		return resultCode.getMsg();
	}

    @Override
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }

}